/*
-------------------------------------------------------------------
BIE is Copyright 2001-2003 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
*/
/**
 *  Data bean representing the configuration of a particular sbie service.
 */
package com.webdeninteractive.sbie.config.data;

import java.lang.StringBuffer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Collection;

public class ServiceData {

    String name;
    String classname;
    long interval = -1; // -1 == run once only
    HashMap params = new HashMap();

    public boolean isValid() {
        return ( name!=null && classname!=null );
    }

    public void setServiceName( String name ) {
        this.name = name;
    }

    public String getServiceName() {
        return this.name;
    }

    public void setServiceClassName( String classname ) {
        this.classname = classname;
    }

    public String getServiceClassName() {
        return this.classname;
    }

    public void setServiceInterval( long interval ) {
        this.interval = interval;
    }

    public long getServiceInterval() {
        return this.interval;
    }

    /** Adds a parameter, as a Parameter object. */
    public void addParameter( Parameter p ) {
        params.put( p.getName(), p );
    }

    /** @return Collection of Parameter objects. */
    public Collection getParameters() {
        return params.values();
    }

    /** Set the parameter with the given name to the given value.
     *  If the given value is null, the parameter will be deleted. */    
    public void setParameter( String name, String value ) {
        if ( value!=null ) {
            Parameter p = new Parameter();
            p.setName(name);
            p.setValue(value);
            params.put( name, p );
        } else {
            params.remove( name );
        }
    }

    /** @return the value of the parameter with the given name,
     *          or null if no such parameter is set for this Client. */
    public String getParameter( String name ) {
        Parameter p = (Parameter)params.get(name);
        if (p!=null) {
            return p.getValue();
        } else {
            return null;
        }
    }

    public String toString() {
        StringBuffer ret = new StringBuffer();
        ret.append( " <service name=\"" + name + "\">\n" );
        ret.append( "  <class>" + classname + "</class>\n" );
        ret.append( "  <interval>" + interval + "</interval>\n" );
        Iterator it = params.values().iterator();
        while(it.hasNext()) {
            ret.append( "  " + it.next() );
        }
        ret.append( " </service>\n" );
        return ret.toString();
    }
}
